package com.wuzhishan.news.controller;

import com.wuzhishan.news.pojo.BlogUser;
import com.wuzhishan.news.pojo.Result;
import com.wuzhishan.news.service.accountService;
import com.wuzhishan.news.service.homeService;
import com.wuzhishan.news.service.userService;
import com.wuzhishan.news.utils.numberTransUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/account")
public class accountController {
    @Autowired
    private userService service;
    @Autowired
    private accountService accountService;
    @Autowired
    private homeService homeService;

    /**
     * restful风格形式，动态判断跳转页面
     * @param name
     * @param session
     * @return
     */
    @RequestMapping(value = "/{name}",method = RequestMethod.GET)
    public Object profile(@PathVariable String name, HttpSession session){
        BlogUser user = (BlogUser) session.getAttribute("loginInfo");
        if(user==null){
            //未登录状态
            return "redirect:/login.html";
        }
        ModelAndView mv = new ModelAndView("/account/"+name);
        return mv;
    }

    /**
     * 修改密码
     * @param oldPassword
     * @param password
     * @param session
     * @return
     */
    @RequestMapping("/password/change")
    @ResponseBody
    public Object updatePassword(String oldPassword,String password ,HttpSession session){
        Result result = service.updatePassword(oldPassword,password,session);
        return result;
    }

    /**
     * 修改用户信息
     * @param nickname
     * @param sign
     * @param session
     * @return
     */
    @RequestMapping("/profile/change")
    public Object updateProfile(String nickname,String sign ,HttpSession session){
        service.updateProfile(nickname,sign,session);
        return "redirect:/account/profile.ftl";
    }

    @RequestMapping(value="/avatar",method= RequestMethod.POST)
    public Object updateAvatar(int x, int y, int width, int height, String path, HttpServletRequest request) {
        Result res=service.updateAvatar(x, y, width, height, path, request);
        int code = (int)res.get("code");
        if(code==1) {
            return "redirect:/login.html";
        }
        if(code==2) {
            return "redirect:/account/avatar";
        }
        return "redirect:/500.html";
    }

    /**
     * 喜欢此文章
     * @return
     */
    @RequestMapping(value="/favor")
    @ResponseBody
    public Object favor(String id, HttpSession session) {
        String articleId = numberTransUtil.TransNum(id);
        Result result = accountService.favor(articleId,session);
        return result;
    }

    /**
     * 取消喜欢此文章
     * @return
     */
    @RequestMapping(value="/unfavor")
    @ResponseBody
    public Object unfavor(String id, HttpSession session) {
        BlogUser user = (BlogUser) session.getAttribute("loginInfo");
        if(user==null){
            //未登录状态
            return "redirect:/login.html";
        }
        String articleId = numberTransUtil.TransNum(id);
        Result result = homeService.unfavor(articleId,user.getId());
        return result;
    }

    /**
     * 关注用户
     * @return
     */
    @RequestMapping(value="/follow",method = RequestMethod.POST)
    @ResponseBody
    public Object follow(String id, HttpSession session) {
        //去除千分位逗号分隔符
        String userId = numberTransUtil.TransNum(id);
        Result result = accountService.follow(userId,session);
        return result;
    }

    /**
     * 判断是否已经关注用户，关注用户
     * @return
     */
    @RequestMapping(value="/follow/check/{id}",method = RequestMethod.POST)
    @ResponseBody
    public Object followCheck(@PathVariable String id, HttpSession session) {
        //去除千分位逗号分隔符
        String userId = numberTransUtil.TransNum(id);
        Result result = accountService.followCheck(userId,session);
        return result;
    }

    /**
     * 取消关注用户
     * @return
     */
    @RequestMapping(value="/unfollow",method = RequestMethod.GET)
    @ResponseBody
    public Object unfollow(String id, HttpSession session) {
        BlogUser user = (BlogUser) session.getAttribute("loginInfo");
        if(user==null){
            //未登录状态
            return "redirect:/login.html";
        }
        Result result = accountService.unfollow(user.getId(),id);
        return result;
    }
    /**
     * 修改邮箱地址
     * @return
     */
    @RequestMapping(value="/email")
    @ResponseBody
    public Object email(String id, HttpSession session) {
        BlogUser user = (BlogUser) session.getAttribute("loginInfo");
        if(user==null){
            //未登录状态
            return "redirect:/login.html";
        }
        Result result = accountService.unfollow(user.getId(),id);
        return result;
    }
}
